<?php
class ControllerAccountForgotten extends Controller {
	private $error = array();

	public function index() {
		if ($this->customer->isLogged()) {
			$this->redirect($this->url->https('account/forgotten'));
		}

		$this->language->load('account/forgotten');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('account/customer');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$password = substr(md5(rand()), 0, 7);

			$subject = sprintf($this->language->get('mail_subject'), $this->config->get('config_store'));

			$message  = sprintf($this->language->get('mail_greeting'), $this->config->get('config_store')) . "\n\n";
                         
                         //--------------11/05/2012 Start edit by Le_Minh sent mail
			$message .= $this->language->get('mail_username');
			$message .=$this->model_account_customer->getUsername($this->request->post['email']). "\n\n";
			//---------------11/05/2012 End edit by Le_Minh sent mail

			$message .= $this->language->get('mail_password') ;
			$message .= $password;
            $mail = new Mail($this->config->get('config_mail_protocol'), $this->config->get('config_smtp_host'), $this->config->get('config_smtp_username'), html_entity_decode($this->config->get('config_smtp_password')), $this->config->get('config_smtp_port'), $this->config->get('config_smtp_timeout'));
			$mail->setTo($this->request->post['email']);
			$mail->setFrom($this->config->get('config_email'));
			$mail->setSender($this->config->get('config_store'));
			$mail->setSubject($subject);
			$mail->setText($message);
			$mail->send();

			$this->model_account_customer->editPassword($this->request->post['email'], $password);

         //   $this->model_account_customer->editusers($this->request->get['user'], $password);
//print_r("de coi sao");


			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->https('account/login'));
		}

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('account/account'),
        	'text'      => $this->language->get('text_account'),
        	'separator' => $this->language->get('text_separator')
      	);

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('account/forgotten'),
        	'text'      => $this->language->get('text_forgotten'),
        	'separator' => $this->language->get('text_separator')
      	);

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_your_email'] = $this->language->get('text_your_email');
		$this->data['text_email'] = $this->language->get('text_email');

		$this->data['entry_email'] = $this->language->get('entry_email');
		//Begin add code by D-Ngan July 13 2012
		$this->data['captcha_notice']=$this->language->get('captcha_notice');
		$this->data['error_captcha_login']=$this->language->get('error_captcha_login');
		//End add code by D-Ngan July 13 2012
		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['button_back'] = $this->language->get('button_back');

		if (isset($this->error['message'])) {
			$this->data['error'] = $this->error['message'];
		} else {
			$this->data['error'] = '';
		}

		$this->data['action'] = $this->url->https('account/forgotten');

		$this->data['back'] = $this->url->https('account/account');

		$this->id       = 'content';
		$this->template = $this->config->get('config_template') . 'account/forgotten.phtml';
		$this->layout   = 'common/layout';

		$this->render();
	}

	private function validate() {
		if(!$this->validCaptcha()){
			$this->error['message'] = 'Bạn nhập sai mã xác nhận, vui lòng kiểm tra lại!';
		}
		if($this->error) return FALSE;
		
		if (!isset($this->request->post['email'])) {
			$this->error['message'] = $this->language->get('error_email');
		} elseif (!$this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
			$this->error['message'] = $this->language->get('error_email');
		}

		if (!$this->error) {
			unset($_SESSION['captcha_val']);
			return TRUE;
		} else {
			return FALSE;
		}
	}
		private function validCaptcha(){
		$kq=TRUE;
		if ($this->request->post['emailValidCaptcha']!=$_SESSION['captcha_val']) {
      	$kq=FALSE;	
    	}
		$_SESSION['wrong_captcha']++;
    	return $kq;
	}
	//End Code by D-Ngan July 13 2012
}
?>